<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/12
 * Time: 17:16
 */

class tagslist_model extends CI_Model
{
    
    public $limit_rows = 15;
    
    public function tags_list($param = array())
    {
        $this->db->from('tags')->order_by('id desc')->limit($this->limit_rows,$this->limit_rows * $param['page']);
        if($param['tags_name']){
            $this->db->where("tags_name LIKE '%{$param['tags_name']}%'");
        }
        $query = $this->db->get();
        $tags = $query->result_array();
        return $tags;
    }
    
    public function count($param = array())
    {
        $this->db->from('tags');
        $this->db->where("tags_name LIKE '%{$param['tags_name']}%'");
        //$this->_where($param);
        
        return $this->db->count_all_results();
    }
    //修改状态
    public function tags_up_show($id){
        $query = $this->db->from('tags')->where(['id'=>$id])->select('is_show')->get();
        $is_show = $query->row_array();
        $is_show['is_show'] == 1 ? $val = 2 : $val = 1;
        $this->db->where(['id'=>$id])->update('tags',['is_show'=>$val]);
    }

    public function tags_add($tags_name){
        $_cate = array(
            'tags_name' => $tags_name,
        );
        $this->db->insert('tags', $_cate);
    }

    //标签查询
    public function tags_find(){
        $this->db->from('tags')->where(['is_show'=>1])->order_by('id desc');
        $query = $this->db->get();
        $tags = $query->result_array();
        return $tags;
    }
    //标签in查询
    public function tags_in($id){
        $goods_sku = $this->db->from('goods_sku')->select('color,size')->where(['goods_id'=>$id,'is_delete'=>0])->get()->result_array();
        $color = array_unique(array_column($goods_sku,'color'));
        $size = array_unique(array_column($goods_sku,'size'));
        // $this->db->from('tags')->select('tags_name')->where_in('id',explode(',',$id));
        // $query = $this->db->get();
        // $tags = $query->result_array();
        
        foreach ($color as $k => $v) {
            $color_s .= $v.',';
        }
        foreach ($size as $k => $v) {
            $size_s .= $v.',';
        }
        $res = $color_s.$size_s;
        return substr($res,0,strlen($res)-1);die;
    }
}